package com.wt.springsamples.security.jwt;

import com.alibaba.fastjson.JSON;
import com.wt.springsamples.common.ApiResult;
import com.wt.springsamples.common.ApiResultCodeEnum;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class JwtAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException {
        System.out.println("进入JwtAccessDeniedHandler的handle方法,表示没有权限");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json");
        //输出结果
        ApiResult result = ApiResult.build(ApiResultCodeEnum.NO_PERMISSION.getCode(),ApiResultCodeEnum.NO_PERMISSION.getMessage());
        response.getWriter().write(JSON.toJSONString(result));
    }
}
